﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace MemoLib
{
    public class ConfigOperator
    {
        public static string GetExcelPath()
        {
            return ConfigurationManager.AppSettings["DataSourceFilePath"];
        }

        public static int GetIntervalMinutes()
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);
        }

        public static bool IfInsertAppTaskToTodoListFlag()
        {
            return ConfigurationManager.AppSettings["InsertAppTaskToTodoList"] == "0" ? false : true;
        }

        public static string GetRemindFlg()
        {
            return ConfigurationManager.AppSettings["AlertMsgFlg"];
        }

        public static void SetRemindFlg()
        {
            string value = ConfigurationManager.AppSettings["AlertMsgFlg"] == "1" ? "0" : "1";
            UpdateAppConfig("AlertMsgFlg", value);
        }

        ///在＊.exe.config文件中appSettings配置节增加一对键、值对  
        ///</summary>  
        ///<param name="newKey"></param>  
        ///<param name="newValue"></param>  
        private static void UpdateAppConfig(string newKey, string newValue)
        {
            bool isModified = false;
            foreach (string key in ConfigurationManager.AppSettings)
            {
                if (key == newKey)
                {
                    isModified = true;
                }
            }

            // Open App.Config of executable  
            Configuration config =
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // You need to remove the old settings object before you can replace it  
            if (isModified)
            {
                config.AppSettings.Settings.Remove(newKey);
            }
            // Add an Application Setting.  
            config.AppSettings.Settings.Add(newKey, newValue);
            // Save the changes in App.config file.  
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of a changed section.  
            ConfigurationManager.RefreshSection("appSettings");
        } 
    }
}
